package com.jacobson.core.utils

import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager


/**
 * ================================================
 *
 *  @author Wucz on 2018-10-17 11:36
 * ================================================
 */
object WrapperUtil {
    fun onAttachedToRecyclerView(recyclerView: RecyclerView,
                                 callback: (layoutManager: GridLayoutManager, oldLookup: GridLayoutManager.SpanSizeLookup?, position: Int) -> Int) {
        val layoutManager = recyclerView.layoutManager
        (layoutManager as? GridLayoutManager)?.let {
            val spanSizeLookup = it.spanSizeLookup
            it.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
                override fun getSpanSize(position: Int): Int {
                    return callback.invoke(it, spanSizeLookup, position)
                }
            }
            it.spanCount = it.spanCount
        }
    }

    fun setFullSpan(holder: RecyclerView.ViewHolder) {
        val lp = holder.itemView.layoutParams
        (lp as? StaggeredGridLayoutManager.LayoutParams)?.isFullSpan = true
    }
}